package quoc.phu;

import InvoikeServices.HeThongInvoike;
import InvoikeServices.Protocol;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gcm.GCMRegistrar;

public class HTTP_ClientActivity extends Activity {

	Button btn;
	TextView txt;
//	AsyncTask<String, Void, String> mDangNhapTask;
	//AsyncTask<String, Integer, String> mDangNhapTask;
	AsyncTask<String, Void, Boolean> mDangNhapTask;	
	private ProgressDialog mProgressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn = (Button) findViewById(R.id.btnReq);
		txt = (TextView) findViewById(R.id.text);
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setMessage("Đang kết nối tới server!");
		mProgressDialog.setButton("Hủy bỏ",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				mDangNhapTask.cancel(true);
				mProgressDialog.dismiss();
				
			}
		});

		String regId = GCMRegistrar.getRegistrationId(this);
		HeThongInvoike.register(this);
		if (regId == "") {
			GCMRegistrar.register(this, Protocol.SENDER_ID);
			
			regId = GCMRegistrar.getRegistrationId(this);
			this.txt.setText("Chưa đăng ký GCM");
		} else {
			this.txt.setText(regId);
		}
		final String rID=regId;
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mDangNhapTask = new AsyncTask<String, Void, Boolean>() {
					@Override
			          protected Boolean doInBackground(String... params) {    
						try{
			                return HeThongInvoike.DangNhap(params[0],params[1],params[2]);
						}catch (Exception e) {
							return false;
						}
			          }      

			          @Override
			          protected void onPostExecute(Boolean result) {
			              //  TextView txt = (TextView) findViewById(R.id.text);
			                txt.setText(result+""); // txt.setText(result);
			               mProgressDialog.dismiss();
			          }

			          @Override
			          protected void onPreExecute() {
			        	  mProgressDialog.show();
			          }
				};
				mDangNhapTask.execute("phu","phu",rID);
			}
		});
	}

	@Override
	protected void onDestroy() {
		GCMRegistrar.onDestroy(this);
		super.onDestroy();
	}
}
